%%******************************************************************************************
%% Replication code
%% Figure 2: Dynamic Technology Manipulation: Trade Policies, Foreign Prices and Innovation
%% Figure 3: Dynamic Technology Manipulation: Home Prices and Innovation
%% "Technological Rivalry and Optimal Dynamic Policy in an Open Economy"
%% by Bai, Jin, and Lu, May 2025
%%******************************************************************************************

xx1= 2; % period 1 is steady state

ccr = 'b';
lwd = 2;
smc =20;

ititle = 1; % 1 plot title
fposition =[100,100,1500,700];


% Ramsey results
rname = 'Ramsey_sH0_SF0.mat';

% Private results
pname = 'private_sH0_SF0.mat';
xx2 =  30 ;
iset_yrange =0; % 1 set y-range


%% Ramsey results
load([rname]);

ncountries=2;
nsectors =1;
n = ncountries;
ns = nsectors;
inorm = 200; % normalization according to the second point


vname={'T1','T2','taux','x1','x2','w1','w2','P2','pi11','pi22','rmul','MULT_4','MULT_3','r1','r2'};

v1 = {'theta1'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
theta1 = M_.params(ttt);


v1 = {'sigmaH'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaH = M_.params(ttt);

v1 = {'sigmaF'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaF = M_.params(ttt);

v1 = {'betad'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
beta = M_.params(ttt);

nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['ri',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    yvar(i,:) = oo_.endo_simul(ivname(i),:);
end

ryvar=yvar;

Tperiod = 1:length(yvar);


%% Together with private equilibrium
% to produce the paper results
load([pname]);

vname={'T1','T2','x1','x2','w1','w2','P2','pi11','pi22','r1','r2','taux'};

nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['pi',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    pyvar(i,:) = oo_.endo_simul(ivname(i),:);
end


%% Figure 2(a) export tax
irow  = 2;
icol  = 4;

figure (2)
header = 'Figure 2: Dynamic Technology Manipulation: Trade Policies, Foreign Prices and Innovation';
findex = 2;
F2 = figure(findex);
set(F2,'numbertitle','off')
set(F2,'name',header)

subplot(irow,icol,1)
yy = ryvar(ritaux,2:end)-pyvar(pitaux,2:end);
ylabel('level difference')

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd,'LineStyle','-')
yline(0,'LineStyle','-.','LineWidth',2)
set(gca,'FontSize',smc)

xlim([xx1,xx2])
ylim([-0.05,0.15])
yticks([-0.05:0.05:0.15])
xticks([0:5:30])

if ititle==1
    title('(a) \tau^x(\sigma=0)')
end
box on


%% Figure 2(b) Foreign real wage
subplot(irow,icol,2)

rww2 = ryvar(riw2,:)-yvar(riP2,:);
pww2 = pyvar(piw2,:)-pyvar(piP2,:);

yy = 100*(rww2(2:end)-pww2(2:end));
ylabel('percent')

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)

set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-0.6,0.6])
yticks([-0.6:0.2:0.6])
xlim([xx1,xx2])
xticks([0:5:30])
box on
if ititle==1
    title('(b) w_2/P_2 (\sigma=0)')
end

%% Figure 2(c) Foreign Real interest rate
subplot(irow,icol,3)
c2 = exp(ryvar(rix2,2:end))./exp(ryvar(riP2,2:end));
p2 = exp(ryvar(riP2,2:end));
Rt2 = (c2(1:end-1)./c2(2:end)).^(-sigmaF)/beta;

c2 = exp(pyvar(pix2,2:end))./exp(pyvar(piP2,2:end));
p2 = exp(pyvar(piP2,2:end));
pRt2 = (c2(1:end-1)./c2(2:end)).^(-sigmaF)/beta;


yy = (Rt2-pRt2)*100;

plot(Tperiod(2:end-1),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
box on

ylim([-0.01,0.05])
yticks([-0.01:0.01:0.05])
xticks([0:5:30])
if ititle==1
    title('(c) R_2(\sigma=0)')
end

%% Figure 2(d) Foreign innovation labor
subplot(irow,icol,4)

yy = 100*(ryvar(rir2,2:end)-pyvar(pir2,2:end));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')
xlim([xx1,xx2])

ylim([-30,30])

yticks([-30:10:30])
xticks([0:5:30])
box on
if ititle==1
    title('(d) L_2^r(\sigma=0)')
end
set(gca,'Fontsize',smc)

set(F2,'position',fposition)

%% Figure 3(a) Home real wage

figure(3)
header = 'Figure 3:  Dynamic Technology Manipulation: Home Prices and Innovation';
findex = 3;
F3 = figure(findex);
set(F3,'numbertitle','off')
set(F3,'name',header)

subplot(irow,icol,1)
rw1 = exp(ryvar(riw1,:));
pw1 = exp(pyvar(piw1,:));

yy = 100*(log(rw1)-log(pw1));

plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')
set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-0.6,0.6])
yticks([-0.6:0.2:0.6])
xticks([0:5:30])
box on
if ititle==1
    title('(a) w_1/P_1 (\sigma=0)')
end


%% Figure 3(b) Home R1
figure(3)
subplot(irow,icol,2)

c1 = exp(ryvar(rix1,2:end));
Rt1 = (c1(1:end-1)./c1(2:end)).^(-sigmaH)/beta;

c1 = exp(pyvar(pix1,2:end));
pRt1 = (c1(1:end-1)./c1(2:end)).^(-sigmaH)/beta;


plot(Tperiod(2:end-1),100*(Rt1-pRt1),'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])

box on

ylim([-0.01,0.05])
yticks([-0.01:0.01:0.05])
xticks([0:5:30])
if ititle==1
    title('(b) R_1(\sigma=0)')
end


%% Figure 3(c) Home innovation labor
figure(3)
subplot(irow,icol,3)

yy = 100*(ryvar(rir1,2:end)-pyvar(pir1,2:end));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percent')

xlim([xx1,xx2])

ylim([-6,8])
yticks([-6:2:8])
xticks([0:5:30])
yline(0,'LineStyle','-.','LineWidth',2)
box on

set(gca,'Fontsize',smc)

title('(c) L_1^r(\sigma=0)')


%% Figure 3(d) Home consumption
figure(3)
subplot(irow,icol,4)
yy = 100*(ryvar(rix1,:)-pyvar(pix1,:));

plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
ylabel('percent')

yline(0,'LineStyle','-.','LineWidth',2)
set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-10,2])
yticks([-10:2:2])
xticks([0:5:30])
box on
title('(d)C_1(\sigma=0)')


%**********************************************
%% Load results for sigma=2.5

rname = 'Ramsey_sH2.5_SF2.5.mat';

% Private results
pname = 'private_sH2.5_SF2.5.mat';
xx2 =150;
iset_yrange =0; % 1 set y-range


%% Ramsey results
load([rname]);

ncountries=2;
nsectors =1;
n = ncountries;
ns = nsectors;
inorm = 200; % normalization according to the second point


vname={'T1','T2','taux','x1','x2','w1','w2','P2','pi11','pi22','rmul','MULT_4','MULT_3','r1','r2'};

v1 = {'theta1'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
theta1 = M_.params(ttt);


v1 = {'sigmaH'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaH = M_.params(ttt);

v1 = {'sigmaF'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
sigmaF = M_.params(ttt);

v1 = {'betad'};
mm=strcmpi(v1,M_.param_names);
ttt=find(mm==1);
beta = M_.params(ttt);



nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['ri',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    yvar(i,:) = oo_.endo_simul(ivname(i),:);
end

ryvar=yvar;

Tperiod = 1:length(yvar);



%% Together with private equilibrium
% to produce the paper results
load([pname]);

vname={'T1','T2','x1','x2','w1','w2','P2','pi11','pi22','r1','r2','taux'};

nv = length(vname);
for i=1:nv
    mm=strcmpi(vname(i),M_.endo_names);
    ttt=find(mm==1);
    if (isempty(ttt)==0)
        ivname(i) = ttt; % declaration order
    else
        ivname(i) =0;

        fprintf(1,'cannot find variable %s\n',char(vname(i)));
    end
    eval(['pi',char(vname(i)),'=',num2str(i),';'])
end

for i=1:nv
    pyvar(i,:) = oo_.endo_simul(ivname(i),:);
end


%% Figure 2(e) export tax, sigma=2.5
figure(2)
subplot(irow,icol,5)
yy = ryvar(ritaux,2:end)-pyvar(pitaux,2:end);
ylabel('level difference')

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd,'LineStyle','-')
yline(0,'LineStyle','-.','LineWidth',2)
set(gca,'FontSize',smc)

xlim([xx1,xx2])
ylim([-0.015,0.015])
yticks([-0.015:0.005:0.015])
xticks([0:20:150])

if ititle==1
    title('(a) \tau^x(\sigma=2.5)')
end
box on


%% Figure 2(f) Foreign real wage, sigma=2.5
figure(2)
subplot(irow,icol,6)

rww2 = ryvar(riw2,:)-yvar(riP2,:);
pww2 = pyvar(piw2,:)-pyvar(piP2,:);

yy = 100*(rww2(2:end)-pww2(2:end));
ylabel('percent')

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)

set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-0.15,0.15])

yticks([-0.15:0.05:0.15])
xticks([0:20:150])
box on
if ititle==1
    title('(b) w_2/P_2 (\sigma=2.5)')
end

%% Figure 2(g) Foreign Real interest rate, sigma=2.5
figure(2)
subplot(irow,icol,7)
c2 = exp(ryvar(rix2,2:end))./exp(ryvar(riP2,2:end));
p2 = exp(ryvar(riP2,2:end));
Rt2 = (c2(1:end-1)./c2(2:end)).^(-sigmaF)/beta;

c2 = exp(pyvar(pix2,2:end))./exp(pyvar(piP2,2:end));
p2 = exp(pyvar(piP2,2:end));
pRt2 = (c2(1:end-1)./c2(2:end)).^(-sigmaF)/beta;


yy = (Rt2-pRt2)*100;

plot(Tperiod(2:end-1),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
box on

ylim([-0.01,0.05])
yticks([-0.01:0.01:0.05])
xticks([0:20:150])
if ititle==1
    title('(c) R_2(\sigma=2.5)')
end

%% Figure 2(h) Foreign innovation labor, sigma=2.5
figure(2)
subplot(irow,icol,8)

yy = 100*(ryvar(rir2,2:end)-pyvar(pir2,2:end));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')
xlim([xx1,xx2])

ylim([-1,0.2])

yticks([-1:0.2:0.2])
xticks([0:20:150])
box on
if ititle==1
    title('(d) L_2^r(\sigma=2.5)')
end
set(gca,'Fontsize',smc)


%% Figure 3(e) Home real wage,\sigma=2.5
figure(3)
subplot(irow,icol,5)
rw1 = exp(ryvar(riw1,:));
pw1 = exp(pyvar(piw1,:));

yy = 100*(log(rw1)-log(pw1));

plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')
set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-0.15,0.15])
yticks([-0.15:0.05:0.15])
xticks([0:20:150])
box on
if ititle==1
    title('(a) w_1/P_1 (\sigma=2.5)')
end


%% Figure 3(f) Home R1,\sigma=2.5
figure(3)
subplot(irow,icol,6)

c1 = exp(ryvar(rix1,2:end));
Rt1 = (c1(1:end-1)./c1(2:end)).^(-sigmaH)/beta;

c1 = exp(pyvar(pix1,2:end));
pRt1 = (c1(1:end-1)./c1(2:end)).^(-sigmaH)/beta;


plot(Tperiod(2:end-1),100*(Rt1-pRt1),'Color',ccr,'LineWidth',lwd)
yline(0,'LineStyle','-.','LineWidth',2)
ylabel('percent')

set(gca,'Fontsize',smc)
xlim([xx1,xx2])
box on

ylim([-0.01,0.05])
yticks([-0.01:0.01:0.05])
xticks([0:20:150])
if ititle==1
    title('(b) R_1(\sigma=2.5)')
end


%% Figure 3(g) Home innovation labor,\sigma=2.5
figure(3)
subplot(irow,icol,7)

yy = 100*(ryvar(rir1,2:end)-pyvar(pir1,2:end));

plot(Tperiod(2:end),yy,'Color',ccr,'LineWidth',lwd)
ylabel('percent')

xlim([xx1,xx2])

ylim([-0.1,0.2])
yticks([-0.1:0.05:0.2])
xticks([0:20:150])
yline(0,'LineStyle','-.','LineWidth',2)
box on

set(gca,'Fontsize',smc)

title('(c) L_1^r(\sigma=2.5)')


%% Figure 3(h) Home consumption,\sigma=2.5
figure(3)
subplot(irow,icol,8)
yy = 100*(ryvar(rix1,:)-pyvar(pix1,:));

plot(Tperiod(2:end),yy(2:end),'Color',ccr,'LineWidth',lwd)
ylabel('percent')

yline(0,'LineStyle','-.','LineWidth',2)
set(gca,'Fontsize',smc)
xlim([xx1,xx2])

ylim([-0.03,0.02])
yticks([-0.03:0.01:0.02])
xticks([0:20:150])
box on
title('(d)C_1(\sigma=2.5)')



set(F2,'position',fposition)
set(F3,'position',fposition)
